﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer.Properties;
using System.Transactions;
using DataAccessLayer.Admin.Copy.IndexMarkStruct;
using IMModel.Domain;

namespace DataAccessLayer.Admin.Copy.Aggregation
{
    public enum CharacteristicCopyMode { WithGradations, WithoutGradations };

    public class CopyIndexMark
    {
        private ICopy copySection = new SectionCopy();
        private ICopy copyCharacteristic = null;

        public CopyIndexMark(CharacteristicCopyMode mode)
        {
            switch (mode)
            {
                case CharacteristicCopyMode.WithGradations:
                    this.copyCharacteristic = new CharacteristicCopyWithGradations();
                    break;
                case CharacteristicCopyMode.WithoutGradations:
                    this.copyCharacteristic = new CharacteristicCopyWithoutGradations();
                    break;
            }
        }

        public void CopyCharacteristic(int IDCopiedCharacteristicTerm, int? IDParentSection, int fromIDTerm, int toIDTerm)
        {
            copyCharacteristic.Copy(IDCopiedCharacteristicTerm, IDParentSection, fromIDTerm, toIDTerm);
        }

        public void StartCopyAll(int fromIDTerm, int toIDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    IEnumerable<SectionTerm> rootsSectionTerms = db.SectionTerms.Where(w => w.IDTerm == fromIDTerm
                        && w.IDParentSectionTerm == null);
                    foreach (SectionTerm st in rootsSectionTerms)
                    {
                        this.HierarchicalCopySection(st.IDSectionTerm, null, fromIDTerm, toIDTerm);
                    }

                    transaction.Complete();
                }
            }
        }

        public void HierarchicalCopySection(int oldIDSectionTerm, int? toIDParentSectionTerm, int fromIDTerm, int toIDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    SectionTerm st = db.SectionTerms.Single(s => s.IDSectionTerm == oldIDSectionTerm);

                    int newIDSectionTerm = copySection.Copy(oldIDSectionTerm, toIDParentSectionTerm, fromIDTerm, toIDTerm);

                    foreach (CharacteristicTerm charTerm in db.CharacteristicTerms.Where(w => w.CategorySectionTerm.IDSectionTerm == oldIDSectionTerm
                        && w.IDTerm == st.IDTerm))
                    {
                        copyCharacteristic.Copy(charTerm.IDCharacteristicTerm, newIDSectionTerm, fromIDTerm, toIDTerm);
                    }

                    foreach (SectionTerm secTerm in db.SectionTerms.Where(w => w.IDParentSectionTerm == oldIDSectionTerm
                        && w.IDTerm == st.IDTerm))
                    {
                        HierarchicalCopySection(secTerm.IDSectionTerm, newIDSectionTerm, fromIDTerm, toIDTerm);
                    }

                    transaction.Complete();
                }
            }
        }
    }
}
